package com.sskj.level.http;


import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.level.bean.AssetListBean;
import com.sskj.level.bean.SocketSendBean;
import com.sskj.level.bean.TradeInfoBean;
import com.sskj.level.bean.TradeListBean;
import com.sskj.level.bean.TradeSetBean;
import com.sskj.level.bean.WinLossBean;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.WSFiveBean;

import java.util.List;
import java.util.concurrent.Executor;

import okhttp3.WebSocket;

/**
 * 作者 :吕志豪
 * 简书：https://www.jianshu.com/u/6e525b929aac
 * github：https://github.com/lvzhihao100
 * 描述：
 * 创建时间：2018-10-15 14:08
 */
public class HttpService {


    /**
     * 首页行情列表
     *
     * @param code 标识符
     * @return
     */
    public GetRequest<HttpData<List<CoinBean>>> getPro(String code) {
        return OkGo.<HttpData<List<CoinBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_PRO)
                .params("code", code);
    }


    /**
     * 盘口
     *
     * @param code 币种标识符
     * @return
     */
    public GetRequest<HttpData<List<WSFiveBean>>> getPankou(String code) {
        return OkGo.<HttpData<List<WSFiveBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_PANKOU)
                .params("code", code);
    }


    public GetRequest<HttpData<TradeInfoBean>> getTradeInfo() {
        return OkGo.<HttpData<TradeInfoBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_TRADE_INFO);
    }

    public PostRequest<HttpData> levelAllPc() {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.LEVEL_ALL_PC);
    }


    public PostRequest<HttpData<PageBean<TradeListBean>>> getChiCang(String code) {
        return OkGo.<HttpData<PageBean<TradeListBean>>>post(HttpConfig.BASE_URL + HttpConfig.CHICANG_LIST)
                .params("code", code);
    }

    public PostRequest<HttpData> pingCang(String num, String orderId) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.PINGCANG)
                .params("num", num)
                .params("order_id", orderId)
                ;
    }

    public PostRequest<HttpData<PageBean<TradeListBean>>> getWeituo(String code) {
        return OkGo.<HttpData<PageBean<TradeListBean>>>post(HttpConfig.BASE_URL + HttpConfig.WEITUO)
                .params("code", code);
    }

    public PostRequest<HttpData<PageBean<TradeListBean>>> getChengjiao(String page, String pageSize, String code) {
        return OkGo.<HttpData<PageBean<TradeListBean>>>post(HttpConfig.BASE_URL + HttpConfig.CHENGJIAO)
                .params("p", page)
                .params("size", pageSize)
                .params("code", code)
                ;
    }

    public GetRequest<HttpData<TradeSetBean>> getTradeSetInfo(String code) {
        return OkGo.<HttpData<TradeSetBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_TRADE_SET_INFO)
                .params("code", code);
    }

    public PostRequest<HttpData> setStop(String orderId, String stopProfitPrice, String stopLossPrice) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SET_POIT)
                .params("h_id", orderId)
                .params("zy", stopProfitPrice)
                .params("zs", stopLossPrice)
                ;
    }

    public PostRequest<HttpData> cancelOrder(String orderId) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.CANCEL_ORDER)
                .params("order_id", orderId)

                ;
    }

    public WebSocketObserver pushCoinFive(String type) {
        return new WebSocketObserver(HttpConfig.WS_FIVE_BASE_URL) {
            @Override
            public void onOpen(WebSocket webSocket, okhttp3.Response response) {
                webSocket.send(new Gson().toJson(new SocketSendBean(type)));
            }
        };
    }

    public PostRequest<HttpData> createOrder(String entrustLot, String buyPrice, String buyBillType, String stockCode, String billPriceType, String stopProfit, String stopLoss, String lever) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.CREATE_ORDER)
                .params("newprice", buyPrice)
                .params("type", billPriceType)
                .params("otype", buyBillType)
                .params("buynum", entrustLot)
                .params("shopname", stockCode)
                .params("zy", stopProfit)
                .params("zs", stopLoss)
                .params("leverage", lever)

                ;
    }


    public GetRequest<HttpData<List<WSFiveBean>>> getDeepData(String code) {
        return OkGo.<HttpData<List<WSFiveBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_DEEP)
                .params("code", code)
                ;
    }

    public PostRequest<HttpData<AssetListBean>> getUsdt() {
        return OkGo.<HttpData<AssetListBean>>post(HttpConfig.BASE_URL + HttpConfig.GET_USDT)
                ;
    }

    public PostRequest<HttpData<WinLossBean>> getWinLoss(String pid) {
        return OkGo.<HttpData<WinLossBean>>post(HttpConfig.BASE_URL + HttpConfig.GET_WIN_LOSS)
                .params("pid", pid)
                ;
    }
}